package com.qh.org.service.impl;

import com.qh.basic.service.impl.BaseServiceImpl;
import com.qh.basic.util.Md5Utils;
import com.qh.basic.util.StrUtils;
import com.qh.basic.util.pageList;
import com.qh.org.domain.Employee;
import com.qh.org.mapper.EmployeeMapper;
import com.qh.org.query.EmployeeQuery;
import com.qh.org.service.EmployeeService;
import com.qh.user.domain.Logininfo;
import com.qh.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
/**
 * 业务实现类：部门
 */
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements EmployeeService {

    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 添加重构
     * @param employee
     */
    @Transactional
    @Override
    public void save(Employee employee) {
        //盐
        String salt = StrUtils.getComplexRandomString(32);
        employee.setSalt(salt);
        //加密加盐
        String md5Password = Md5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setPassword(md5Password);
        //先添加logininfo
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setType(0);//管理员
        logininfo.setDisable(true);
        logininfoMapper.save(logininfo);
        //在添加t_employee_role

        employeeMapper.saveRole(employee);
        //再添加employee
        employee.setLogininfo_id(logininfo.getId());
        super.save(employee);
    }

    /**
     * 删除重构
     * @param id
     */
    @Transactional
    @Override
    public void del(Long id) {
        Employee employee = employeeMapper.loadOne(id);
        Long logininfo_id = employee.getLogininfo_id();
        //先删logininfo
        logininfoMapper.del(logininfo_id);
        //再删t_employee_role
        employeeMapper.delRoleByEmployeeId(id);
        //再删employee
        super.del(id);
    }

    /**
     * 修改重构
     * @param employee
     */
    @Transactional
    @Override
    public void update(Employee employee) {
        //盐
        String salt = StrUtils.getComplexRandomString(32);
        employee.setSalt(salt);
        //加密加盐
        String md5Password = Md5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setPassword(md5Password);
        Logininfo logininfo = employee2Logininfo(employee);
        Employee dbEmployee = employeeMapper.loadOne(employee.getId());
        //重新设置Logininfoid
        logininfo.setId(dbEmployee.getLogininfo_id());
        Logininfo dbLogininfo = logininfoMapper.loadOne(dbEmployee.getLogininfo_id());
        logininfo.setDisable(dbLogininfo.getDisable());

        //操作数据库
        logininfoMapper.update(logininfo);
        //employee - 前端传递的
        employee.setShop_id(dbEmployee.getShop_id());
        employee.setLogininfo_id(dbEmployee.getLogininfo_id());
        super.update(employee);

        //修改 = 先删再添加
        employeeMapper.delRoleByEmployeeId(employee.getId());
        employeeMapper.saveRole(employee);
    }

    @Override
    public void patchDel(List<Long> ids) {
        for (Long id : ids) {
            del(id);
        }
    }

    private Logininfo employee2Logininfo(Employee employee) {
        Logininfo logininfo = new Logininfo();
        //通过属性同名原则进行拷贝
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setType(0);
        logininfo.setDisable(true);
        return logininfo;
    }


    /**
     * 查询店铺的所有的管理员通过shopId
     * @param shopId
     */
    @Override
    public List<Employee> queryByShopId(Long shopId) {
        return employeeMapper.queryByShopId(shopId);
    }
}
